import dayjs from 'dayjs'
import {
  WARNING_LEVELS,
  WARNING_STATES,
  WARNING_WORK_ORDER_STATES,
} from '@/enum'

/**
 * 计算持续时间
 */
export function calculateTheLastTime(
  date: string,
  now?: string,
) {
  const start = dayjs(date)
  const end = now ? dayjs(now) : dayjs()
  const diff = end.diff(start, 'millisecond')

  const days = Math.floor(diff / (1000 * 60 * 60 * 24))
  const hours = Math.floor(
    (diff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60),
  )
  const minutes = Math.floor(
    (diff % (1000 * 60 * 60)) / (1000 * 60),
  )

  return `${days}天${hours}小时${minutes}分`
}

/**
 * 拼接阈值
 */
export function joinThresoldValue(
  up: number,
  down: number,
  unit: string,
) {
  return `${down ?? 0}-${up ?? 0}（${unit}）`
}

/**
 * 拼接监测值
 */
export function joinWarningValue(
  val: number,
  unit: string,
) {
  return `${val ?? 0} ${unit ? `(${unit})` : ''}`
}

/**
 * 告警类型（等级）name 值
 */
export const WARNING_LEVELS_TEXT = {
  NORMAL: '一般',
  SERIOUS: '严重',
  URGENT: '紧急',
}

/**
 * 告警类型（等级）
 */
export const warningLevels = [
  {
    label: WARNING_LEVELS_TEXT.NORMAL,
    value: WARNING_LEVELS.NORMAL,
  },
  {
    label: WARNING_LEVELS_TEXT.SERIOUS,
    value: WARNING_LEVELS.SERIOUS,
  },
  {
    label: WARNING_LEVELS_TEXT.URGENT,
    value: WARNING_LEVELS.URGENT,
  },
]

/**
 * 告警信息状态 name 值
 */
export const WARNING_STATES_TEXT = {
  UNSETTLED: '未处理',
  SETTLING: '处理中',
  PENDING: '待审核',
  SOLVED: '已处理',
  IGNORED: '已忽略',
}

/**
 * 实时告警的状态
 */
export const realtimeWarningStates = [
  {
    label: WARNING_STATES_TEXT.UNSETTLED,
    value: WARNING_STATES.UNSETTLED,
  },
  {
    label: WARNING_STATES_TEXT.SETTLING,
    value: WARNING_STATES.SETTLING,
  },
  {
    label: WARNING_STATES_TEXT.PENDING,
    value: WARNING_STATES.PENDING,
  },
]

/**
 * 告警历史的状态
 */
export const warningInformationStates = [
  {
    label: WARNING_STATES_TEXT.SOLVED,
    value: WARNING_STATES.SOLVED,
  },
  {
    label: WARNING_STATES_TEXT.IGNORED,
    value: WARNING_STATES.IGNORED,
  },
]

/**
 * 告警工单状态 name 值
 */
export const WARNING_WORK_ORDER_STATES_TEXT = {
  UNSETTLED: '待处理',
  PENDING: '待审核',
  SOLVED: '已处理',
  REJECTED: '已驳回',
}

/**
 * 告警工单状态
 */
export const workOrderStates = [
  {
    label: WARNING_WORK_ORDER_STATES_TEXT.UNSETTLED,
    value: WARNING_WORK_ORDER_STATES.UNSETTLED,
  },
  {
    label: WARNING_WORK_ORDER_STATES_TEXT.PENDING,
    value: WARNING_WORK_ORDER_STATES.PENDING,
  },
  {
    label: WARNING_WORK_ORDER_STATES_TEXT.SOLVED,
    value: WARNING_WORK_ORDER_STATES.SOLVED,
  },
  {
    label: WARNING_WORK_ORDER_STATES_TEXT.REJECTED,
    value: WARNING_WORK_ORDER_STATES.REJECTED,
  },
]
